/**
 * Created by zf233 on 2020/2/10
 */
window.onload = function () {
    var height_total = $(document).height();
    var widht_total = $(document).width();
    var head_title_height = $("#head_title").height();
    var foot_title_height = $("#foot_title").height();
    var menu_div_height = $("#menu_div").height();
    var height = height_total - (head_title_height + foot_title_height) - 10;
    var table_height = height_total - (head_title_height + foot_title_height + menu_div_height) - 50;
    $(".center_detail").height(height);
    $("table tbody").height(table_height);
}
window.onresize = function () {
    var height_total = $(document).height();
    var widht_total = $(document).width();
    var head_title_height = $("#head_title").height();
    var foot_title_height = $("#foot_title").height();
    var menu_div_height = $("#menu_div").height();
    var height = height_total - (head_title_height + foot_title_height) - 10;
    var table_height = height_total - (head_title_height + foot_title_height + menu_div_height) - 50;
    $(".center_detail").height(height);
    $("table tbody").height(table_height);
}

var move_parentId = -1;
var lastFolderName = "根目录"
$(function () {
    function isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }
    var clear_left_menu_back = function () {
        $("#all").click();
    }
    var absolutePath = new Vue({
        el: "#absolutePath_li",
        data: {
            absolutePath: []
        },
        methods: {
            open_folder: function (parentid) {
                getList(parentid, null, null);
            }
        }
    })
    var fileItemsTable = new Vue({
        el: '#file_table',
        data() {
            return {
                files: [],
                recycle: 0
            }
        },
        methods: {
            getClassTr: function (isFolder) {
                if (isFolder == 0) {
                    return "fileItem"
                } else {
                    return "folderItem"
                }
            },
            getClassCheckBox: function (isFolder) {
                if (isFolder == 0) {
                    return "fileCheckBox"
                } else {
                    return "folderCheckBox"
                }
            },
            getClassA: function (isFolder) {
                if (isFolder == 0) {
                    return "share_A"
                } else {
                    return "open_A"
                }
            },
            getEvent: function (isFolder, fileId, fileName) {
                if (this.recycle == 0) {
                    this['fun' + isFolder](fileId, fileName)
                }
            },
            fun0: function (fileId, fileName) {
                open_share_form(fileId, fileName);
            },
            fun1: function (fileId) {
                getList(fileId, null, null);
            },
            setRecycle(value) {
                this.recycle = value;
            },
            setFiles(files) {
                this.files = files
            },
            update(fileId) {
                open_info_modal(fileId);
            },
            choice_tatal() {
                choice_tatal()
            }
        }
    });

    var getList = function (parentid, sortFlag, sortType) {
        $("#remove_add_row").click();
        fileItemsTable.recycle = 0;
        $.ajax({
            async: true,
            url: "/file/list",
            data: {
                parentid: parentid,
                sortFlag: sortFlag,
                sortType: sortType
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = data.absolutePath;
                    choice_tatal();
                    clear_choice();
                    clear_left_menu_back();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var getListClassify = function (classify) {
        fileItemsTable.recycle = 0;
        $("#remove_add_row").click();
        $.ajax({
            async: true,
            url: "/file/classify",
            data: {
                classify: classify
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var getListRecycle = function (classify) {
        fileItemsTable.recycle = 1;
        $("#remove_add_row").click();
        $.ajax({
            async: true,
            url: "/file/recycle/list",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var countdown;
    function timeout() {
        count = 30;
        clearInterval(countdown);
        countdown = setInterval(CountDown, 1000);
        function CountDown() {
            $("#acceptAgreeSpan").text("同意" + "（" + count + "）");
            if (count == 0) {
                $("#acceptAgreeSpan").text("同意");
                clearInterval(countdown);
            }
            count--;
        }
    }

    var getCurrentUser = function () {
        $.ajax({
            async: true,
            url: "/user/current/user/get",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    $("#user_head").attr("src", data.data.headUrl).load();
                    $("#displayName_span").text(data.data.displayName);
                    $("#level_span").text('Lv ' + data.data.level);
                    if (data.data.roleNum == 1) {
                        $("#admin_consul").show();
                    } else {
                        $("#admin_consul").hide();
                    }
                    $("#progress").val(data.data.useCapacityNum);
                    $("#progress").attr("max", data.data.capacityNum);
                    if (data.data.useCapacityNum / data.data.capacityNum > 0.9) {
                        $("#progress").addClass("progress_full");
                    } else {
                        $("#progress").removeClass("progress_full");
                    }
                    $("#useCapacity_span").text(data.data.useCapacity);
                    $("#capacity_span").text(data.data.capacity);
                    $("#freeDownloadCountSpan").text(data.data.availableDownloadCapacity);
                    if (data.data.acceptAgree != 1) {
                        $('#myModal').modal('show')
                        timeout();
                    }
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取用户信息失败");
            },
            type: "post"
        });
    }

    getList(null, null, null);
    getCurrentUser();

    $(".list_files").click(function () {
        var parentid = $(this).attr("value");
        getList(parentid, null, null);
    })
    $(".list_file_classify").click(function () {
        var classify = $(this).attr("id");
        getListClassify(classify);
    })
    $("#recycle").click(function () {
        getListRecycle();
    })

    function getSelectItem() {
        var elementArray = [0];
        var itemCount = 0;
        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        $(".folderCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount > 0) {
            return elementArray;
        }
        return null;
    }

    function getSelectItemOfFile() {
        var elementArray = [0];
        var itemCount = 0;
        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount > 0) {
            return elementArray;
        }
        return null;
    }

    $("#getBig").click(function () {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("暂未开放，敬请期待！");
    })

    $("#update_user_head").change(function () {
        upload();
    });

    function upload() {
        if ($("#update_user_head").val() == '') {
            return;
        }
        if (document.getElementById('update_user_head').files[0].size > 1000000) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("请选择1MB内的头像文件");
            return;
        }
        var formData = new FormData();
        formData.append('headPhoto', document.getElementById('update_user_head').files[0]);
        $.ajax({
            async: true,
            url: "/file/update/head",
            data: formData,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    getCurrentUser();
                    show_userInfo();
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("更新头像成功");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("更新头像失败");
            },
            type: "post"
        });
    }

    $("#fuzzySelect").click(function () {
        $("#selectAll").attr("checked", false);
        clear_choice();
        $("#remove_add_row").click();
        var match = $("#matchCode").val().trim();
        if (isEmpty(match)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("搜索关键字不可以为空");
            return;
        }
        fileItemsTable.recycle = 0;
        $.ajax({
            async: true,
            url: "/file/search",
            data: {
                matchCode: match
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                    clear_left_menu_back();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件搜索失败");
            },
            type: "post"
        });
    });

    var clear_choice = function () {
        $(".fileCheckBox").each(function (index, element) {
            $(element).prop("checked", false);
        });
        $(".folderCheckBox").each(function (index, element) {
            $(element).prop("checked", false);
        });
        $("#selectAll").attr("checked", false);
        $("#choice_total").hide();
    }

    function choice_tatal() {
        var isSelectAll = true;
        var choice_number = 0;
        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
            } else {
                isSelectAll = false;
            }
        });
        $(".folderCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
            } else {
                isSelectAll = false;
            }
        });
        if (choice_number > 0) {
            $("#choice_total").show();
            $("#choice_number").text(choice_number);
        } else {
            $("#choice_total").hide();
        }
        $("#selectAll").prop("checked", isSelectAll);
    }

    function choice_tatal_share() {
        var isSelectAll = true;
        var choice_number = 0;
        $(".shareCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
            } else {
                isSelectAll = false;
            }
        });
        if (choice_number > 0) {
            $("#choice_total_share").show();
            $("#choice_number_share").text(choice_number);
        } else {
            $("#choice_total_share").hide();
        }
        $("#select_all_share").prop("checked", isSelectAll);
    }

    $("#selectAll").click(function () {
        var status = $(this).prop("checked");
        $(".fileCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });
        $(".folderCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });
        if (status) {
            $(".fileItem").each(function (index, element) {
                $(element).css("background", "rgb(245, 251, 255)")
            });
            $(".folderItem").each(function (index, element) {
                $(element).css("background", "rgb(245, 251, 255)")
            })
        } else {
            $(".fileItem").each(function (index, element) {
                $(element).css("background", "")
            });
            $(".folderItem").each(function (index, element) {
                $(element).css("background", "")
            })
        }
        choice_tatal();
    });

    $("#restore").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("是否放回选中项目?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/restore",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getListRecycle();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("放回失败");
            },
            type: "post"
        })
    });

    $("#delete").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("彻底移除选中项目?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/remove",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getListRecycle();
                    getCurrentUser();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("移除失败");
            },
            type: "post"
        })
        getCurrentUser();
    });

    $("#flushRecycle").click(function () {
        if (fileItemsTable.files == null || fileItemsTable.files.length == 0) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.info("废纸篓已经很干净了哦");
            return;
        }
        if (confirm("是否倾倒废纸篓")) {
            $.ajax({
                async: true,
                dataType: "json",
                url: "/file/recycle/flush",
                traditional: true,
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        getListRecycle();
                        getCurrentUser();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("移除失败");
                },
                type: "post"
            })
        }
    });

    $("#downloadSelectAll").click(function () {
        var fileIdArray = getSelectItemOfFile();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中文件项");
            return
        }
        if (!confirm("是否下载选中项?")) {
            return
        }
        for (let i = 0; i < fileIdArray.length; i++) {
            $.ajax({
                async: true,
                data: { fileid: fileIdArray[i] },
                dataType: "json",
                url: "/file/download",
                success: function (data) {
                    if (data.status == 200) {
                        try {
                            var elemIF = document.createElement("iframe");
                            elemIF.src = data.data;
                            elemIF.style.display = "none";
                            document.body.appendChild(elemIF);
                            getCurrentUser();
                        } catch (e) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.error("下载异常！");
                        }
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("下载失败");
                },
                type: "post"
            })
        }
    });

    $("#recycleSelectAll").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("是否将选中项移至废纸篓?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/recycle",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("移至废纸篓失败");
            },
            type: "post"
        })
    });

    $("#deleteSelectAll").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("是否将选中项彻底移除?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/remove",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                    getCurrentUser();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("移除失败");
            },
            type: "post"
        })
        getCurrentUser();
    });

    var share_file_id;
    var default_qr_img_url = "https://www.xcloud.show/static/img/share_default.png";
    function open_share_form(fileId, FileName) {
        share_file_id = fileId;
        var fileNameEXName = FileName;
        var fileName = fileNameEXName.substr(0, fileNameEXName.lastIndexOf("."));
        if (fileName.length > 10) {
            fileNameEXName = fileName.substr(0, 20) + '···' + fileNameEXName.substr(fileNameEXName.lastIndexOf("."));
        }
        $("#share_file_name_h").text('文件分享 - ' + fileNameEXName);
        $("#share_link_input").val("");
        $("#share_qr_img").attr("src", default_qr_img_url).load();
        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
            },
            dataType: "json",
            url: "/share/info",
            success: function (data) {
                if (data.status == 200) {
                    if (data.data.qrLinkStatus == 1) {
                        if (data.data.qrLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享二维码成功");
                            $("#share_qr_img").attr("src", data.data.qrLink).load();
                        }
                    } else if (data.data.qrLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("分享二维码已失效，重新生成或延期");
                    }
                    if (data.data.shareLinkStatus == 1) {
                        if (data.data.shareLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享链接成功");
                            $("#share_link_input").val(data.data.shareLink);
                        }
                    } else if (data.data.shareLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("分享链接已失效，重新生成或延期");
                    }
                } else {
                    if (data.msg != "此文件未分享") {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                }
            },
            error: function () {
            },
            type: "post"
        })
        $("#show_file_share_modal").click();
    }

    function open_info_modal(fileId) {
        update_fileid = fileId;
        $.ajax({
            async: true,
            data: { fileid: fileId },
            dataType: "json",
            url: "/file/detail",
            success: function (data) {
                if (data.status == 200) {
                    $("#update_file_icon").attr('src', data.data.logoUrl).load();
                    if (data.data.folder == 0) {
                        $("#update_name_span").val(data.data.fileName.substr(0, data.data.fileName.lastIndexOf(".")));
                        $("#update_type_span").text(data.data.fileName.substr(data.data.fileName.lastIndexOf(".") + 1));
                    } else {
                        $("#update_name_span").val(data.data.fileName);
                        $("#update_type_span").text("文件夹");
                    }
                    $("#update_size_span").text(data.data.fileSize);
                    $("#update_time_span").text(data.data.uploadTime);
                    update_fileName = data.data.fileName;
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件信息失败");
            },
            type: "post"
        })
        $('#show_file_info_modal').click();
    }

    $("#create_share_link").click(function () {
        var timeout;
        if ($("#timeout3").prop("checked") == true) {
            timeout = 3;
        }
        if ($("#timeout7").prop("checked") == true) {
            timeout = 7;
        }
        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
                type: 1,
                timeout: timeout
            },
            dataType: "json",
            url: "/share/file",
            success: function (data) {
                if (data.status == 200) {
                    $("#share_link_input").val(data.data);
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("生成分享链接成功");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享失败");
            },
            type: "post"
        })
    })
    $("#create_share_qr").click(function () {
        var timeout;
        if ($("#timeout3").prop("checked") == true) {
            timeout = 3;
        }
        if ($("#timeout7").prop("checked") == true) {
            timeout = 7;
        }
        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
                type: 0,
                timeout: timeout
            },
            dataType: "json",
            url: "/share/file",
            success: function (data) {
                if (data.status == 200) {
                    $("#share_qr_img").attr("src", data.data).load();
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("生成分享二维码成功，扫一扫获取");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享失败");
            },
            type: "post"
        })
    })

    var copyBtn = new ClipboardJS('.btn');
    copyBtn.on("success", function (e) {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("分享链接已复制至剪贴板");
        $("#share_link_input").blur();
    });
    copyBtn.on("error", function (e) {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("分享链接复制失败，请手动复制");
        $("#share_link_input").blur();
    });
    $("#share_link_input").focus(function () {
        if (!isEmpty($(this).val())) {
            $("#copy_content").click();
        }
    });

    initFileInput("asy_bs_upload");
    $("#asy_bs_upload").on("fileuploaded", function (event, data, previewId, index) {
        var result = data.response;
        if (result.status == 200) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            var number = parseInt(index) + 1;
            toastr.info('第 ' + number + ' 个文件' + result.msg + '，刷新页面或关闭上传窗口以展示');
            getList();
            getCurrentUser();
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error(result.msg);
        }
    });
    $("#asy_bs_upload").on("filebatchuploadsuccess", function (event, data, previewId, index) {
        var result = data.response;
        if (result.status == 200) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.info(result.msg + '，刷新页面或关闭上传窗口以展示');
            getList();
            getCurrentUser();
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error(result.msg);
        }
    });
    $("#asy_bs_upload").on("filebatchpreupload", function (event, data) {
        var sizeCount = 0;
        for (let i = 0; i < data.files.length; i++) {
            sizeCount += data.files[i].size;
        }
        var value = $("#progress").val();
        var max = $("#progress").attr("max");
        if (sizeCount > (max - value)) {
            return {
                message: "可用空间不足，清理后以继续上传",
                data: {}
            };
        }
    });

    var update_fileid;
    var update_fileName;
    $("#update_detail_submit").click(function () {
        var newFileName = $("#update_name_span").val();
        if (isEmpty(newFileName)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("文件名不可为空");
            return;
        }
        if (update_fileName != newFileName) {
            $.ajax({
                async: true,
                data: {
                    fileid: update_fileid,
                    newFileName: newFileName
                },
                dataType: "json",
                url: "/file/rename",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        update_fileName = newFileName;
                        getList();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件重命名失败");
                },
                type: "post"
            })
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("未做任何修改");
        }
    })

    $("#update_name_span").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#update_detail_submit").click();
        }
    })

    $("#move_button").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("确定要移动吗?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                parentId: move_parentId,
                fileId: fileIdArray
            },
            dataType: "json",
            traditional: true,
            url: "/file/move",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                    getCurrentUser();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件移动失败");
            },
            type: "post"
        })
        $(".fileItem").each(function (index, element) {
            $(element).css("background", "")
        });
        $(".folderItem").each(function (index, element) {
            $(element).css("background", "")
        })
    });

    function init_move_form(parentId) {
        move_parentId = parentId;
        $.ajax({
            async: true,
            data: { parentId: parentId },
            dataType: "json",
            url: "/file/list/lower",
            success: function (data) {
                if (data.status == 200) {
                    flush_move_form(data.data.parentId, data.data.folderVo, data.data.absolutePath)
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件夹列表失败");
            },
            type: "post"
        })
    }

    $("#move_form_open_absolutePath_home").click(function () {
        lastFolderName = "根目录";
        init_move_form(-1);
    })

    function flush_move_form(parentId, folderVo, absolutePath) {
        move_parentId = parentId;
        $(".folder_record_tr").remove();
        $(".move_file_absolute").remove();
        $.each(folderVo, function (index, element) {
            var record = "<tr class='" + "folder_record_tr" + "'><td style='text-align: left; white-space: nowrap;text-overflow: ellipsis;overflow: hidden;'><a href='javascript:void(0)' id='" + element.id + "' class='move_form_open_folder_a' style='margin-left: 20px'>" + element.fileName + "</a></td><td>" + element.uploadTime + "</td></tr>";
            $("#move_form_table").append(record);
        })
        if (folderVo.length == 0) {
            var record = "<tr class='" + "folder_record_tr" + "'><td><span class='glyphicon glyphicon-exclamation-sign' style='font-size: 14px; color: rgb(117, 131, 186);'></span>&nbsp;&nbsp;无文件夹</td></tr>";
            $("#move_form_table").append(record);
        }

        $.each(absolutePath, function (index, element) {
            var folderName = element.folderName;
            if (folderName.length > 10) {
                folderName = folderName.substr(0, 10) + '...';
            }
            var record;
            if (index == absolutePath.length - 1) {
                record = "<li class='divider move_file_absolute'><a id='" + element.parentid + "' class='move_form_open_absolutePath' style='text-decoration:none;'><span style='color:rgb(185,185,185)'>" + folderName + "</span></a></li>";
                lastFolderName = folderName;
            } else {
                record = "<li class='divider move_file_absolute'><a id='" + element.parentid + "' class='move_form_open_absolutePath' style='text-decoration:none;'><span>" + folderName + "</span></a></li>"
            }
            $("#move_file_absolutePath").append(record);
        })

        $(".move_form_open_folder_a").click(function () {
            init_move_form($(this).attr("id"));
        })

        $(".move_form_open_absolutePath").click(function () {
            init_move_form($(this).attr("id"));
        })
    }

    $("#copy_button").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("确定要复制吗?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                parentId: move_parentId,
                fileId: fileIdArray
            },
            dataType: "json",
            traditional: true,
            url: "/file/copy",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                    getCurrentUser();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件复制失败");
            },
            type: "post"
        })
        $(".fileItem").each(function (index, element) {
            $(element).css("background", "")
        });
        $(".folderItem").each(function (index, element) {
            $(element).css("background", "")
        })
    });

    $("#archive_qr_img").click(function () {
        $("#choice_share_ar").click()
    })
    $("#choice_share_ar").change(function () {
        // 构造一个文件渲染对象
        var reader = new FileReader();
        // 得到文件列表数组
        fileObj = $(this)[0].files[0];
        // 拿到文件数据
        reader.readAsDataURL(fileObj);

        reader.onload = function () {
            // 获取文件信息
            imgData = reader.result;
            // 指定位置显示图片
            $("#archive_qr_img").attr("src", imgData);

        };
    })
    $("#archive").click(function () {
        var archive_type;
        if ($("#share_type_0").prop("checked") == true) {
            archive_type = 0;
        }
        if ($("#share_type_1").prop("checked") == true) {
            archive_type = 1;
        }
        if (archive_type == 0) {
            if ($("#choice_share_ar").val() == '') {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("请选择文件分享二维码");
                return;
            }
            if (document.getElementById('choice_share_ar').files[0].size > 1000000) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("请选择1MB内的二维码文件");
                return;
            }
            var formData = new FormData();
            formData.append('qr', document.getElementById('choice_share_ar').files[0]);
            formData.append('parentId', move_parentId);
            formData.append('type', "0");
            $.ajax({
                async: true,
                url: "/file/archive",
                data: formData,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        getList();
                        getCurrentUser();
                        clear_choice();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function (data) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件转存失败");
                },
                type: "post"
            });
        } else if (archive_type == 1) {
            var archiveUrl = $("#archive_share_url").val();
            if (isEmpty(archiveUrl)) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("请输入分享链接");
                return;
            }
            $.ajax({
                async: true,
                data: {
                    url: archiveUrl,
                    parentId: move_parentId,
                    type: 1
                },
                dataType: "json",
                url: "/file/archive",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        getList();
                        getCurrentUser();
                        clear_choice();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件转存失败");
                },
                type: "post"
            })
        }
    })

    $("#folderName").keyup(function () {
        alert("aaa")
        var name = $(this).val().replace(/\s+/g, "");
        if (!isEmpty(name) && name.length < 32 || name.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })

    var add_flag = true;
    var add_row = function () {
        var name = $("#folderName").val().replace(/\s+/g, "");
        if (!isEmpty(name) && name.length < 32) {
            $.ajax({
                async: true,
                data: { foldername: name },
                dataType: "json",
                url: "/file/createfolder",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        remove_add_row();
                        getList();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("新建文件夹失败");
                },
                type: "post"
            })
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("文件夹名格式有误！格式：32位内字符");
        }
    }

    var remove_add_row = function () {
        $("#add_row").remove();
        add_flag = true;
    }

    $("#addFolderButton").click(function () {
        if (add_flag) {
            var row = "<tr id='add_row'>" +
                "<td colspan='2'>" +
                "<input id='folderName' type='text' name='foldername' placeholder='新建文件夹'>" +
                "<a id='add_row' href='javascript:void(0)' style='margin-left: 5px'>" +
                "<span><img src='https://www.xcloud.show/static/img/xcloud_front/enter.png' style='height: 22px'></span>" +
                "</a>" +
                "<a id='remove_add_row' href='javascript:void(0)' style='margin-left: 1px'>" +
                "<span><img src='https://www.xcloud.show/static/img/xcloud_front/esc.png' style='height: 20px'></span>" +
                "</a>" +
                "</td>" +
                "<td><span style='font-size: 10px; color: rgb(146, 146, 146)'>-</span></td>" +
                "<td><span style='font-size: 10px; color: rgb(146, 146, 146)'>-</span></td>" +
                "<td colspan='2'></td>" +
                "<td style='width: 9%'></td>" +
                "</tr>";
            $("#file_table tbody").prepend(row);
            add_flag = false;
            $("#folderName").click(function () {
                return false;
            })
            $("#folderName").blur(function () {
                return false;
            })
            $("#folderName").focus(function () {
                return false;
            })
            $("#folderName").bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    $("#add_row").click();
                }
            });
            $("#remove_add_row").click(function () {
                event.stopPropagation();
                remove_add_row();
            })
            $("#add_row").click(function () {
                event.stopPropagation();
                add_row();
            })
        }
        $("#folderName").focus();
    })

    $("#sort_type").change(function () {
        var flag = $(this).val()
        getList(null, flag, 0);
    })

    function add_share_timeout(shareId, type) {
        $.ajax({
            async: true,
            data: {
                shareId: shareId,
                type: type
            },
            dataType: "json",
            url: "/share/extend",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    flush_share_list_table();
                    clear_choice_share();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                    clear_choice_share();
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享移除失败");
            },
            type: "post"
        })
    }

    var table = new Vue({
        el: "#share_table",
        data: {
            shares: []
        },
        methods: {
            choice_tatal_share() {
                choice_tatal_share()
            },
            getClass() {
                return "shareCheckBox"
            },
            addTimeout(shareId, type) {
                add_share_timeout(shareId, type)
            },
            showShareInfo(id) {
                show_share_info_modal(id);
            }
        }
    })

    function show_share_info_modal(id) {
        $.ajax({
            async: true,
            data: {
                fileid: id,
            },
            dataType: "json",
            url: "/share/info",
            success: function (data) {
                var fileNameEXName = data.data.fileName;
                var fileName = fileNameEXName.substr(0, fileNameEXName.lastIndexOf("."));
                if (fileName.length > 6) {
                    fileNameEXName = fileName.substr(0, 6) + '···' + fileNameEXName.substr(fileNameEXName.lastIndexOf("."));
                }
                $("#share_info_file_name_h").text('查看分享 - ' + fileNameEXName);
                $("#share_info_qr_img").attr("src", default_qr_img_url).load();
                $("#share_info_link_input").val('');
                if (data.status == 200) {
                    if (data.data.qrLinkStatus == 1) {
                        if (data.data.qrLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享二维码成功");
                            $("#share_info_qr_img").attr("src", data.data.qrLink).load();
                        }
                    } else if (data.data.qrLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("分享二维码已失效，重新生成或延期");
                        $("#share_info_qr_img").attr("src", default_qr_img_url).load();
                    }
                    if (data.data.shareLinkStatus == 1) {
                        if (data.data.shareLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享链接成功");
                            $("#share_info_link_input").val(data.data.shareLink);
                        }
                    } else if (data.data.shareLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("分享链接已失效，重新生成或延期");
                        $("#share_info_link_input").val('');
                    }
                } else {
                    if (data.msg != "此文件未分享") {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                }
            },
            error: function () {
            },
            type: "post"
        })
        $("#show_share_info_modal").click();
    }

    $("#select_all_share").click(function () {
        var status = $(this).prop("checked");
        $(".shareCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });
        choice_tatal_share();
    })

    function flush_share_list_table() {
        $.ajax({
            async: true,
            url: "/share/list",
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("获取文件分享列表成功");
                    table.shares = data.data;
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件分享列表失败");
            },
            type: "post"
        });
    }

    $("#share").click(function () {
        flush_share_list_table();
    })

    var clear_choice_share = function () {
        $(".shareCheckBox").each(function (index, element) {
            $(element).prop("checked", false);

        });
        $("#select_all_share").attr("checked", false);
        $("#choice_total_share").hide();
    }

    $("#remove_select_share").click(function () {

        var elementArray = [0];
        var itemCount = 0;

        $(".shareCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount == 0) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中分享项");
            return
        }
        if (!confirm("是否将选中的分享彻底移除?")) {
            return
        }

        $.ajax({
            async: true,
            data: {
                shareId: elementArray
            },
            dataType: "json",
            url: "/share/remove",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    flush_share_list_table();
                    clear_choice_share();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                    clear_choice_share();
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享移除失败");
            },
            type: "post"
        })
    });
    $("#open_user_info_model").click(function () {
        $("#openUserDetailButton").click();
        $('#user_menu').modal('hide')
    })
    $("#user_input_head").click(function () {
        $("#update_user_head_menu").click();
    })
    function show_userInfo() {
        $.ajax({
            async: true,
            url: "/user/current/user/get",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    $("#user_input_head").attr("src", data.data.headUrl).load();
                    $("#user_input_displayName").text(data.data.displayName);
                    $("#user_input_nickname").val(data.data.nickname);
                    $("#user_input_email").val(data.data.email);
                    $("#user_input_phone").val(data.data.phone);

                    $("#user_input_email_code").val("");
                    $("#user_input_phone_code").val("");

                    $("#user_input_uid").text(data.data.uid);
                    $("#user_input_role").text(data.data.role + '/' + data.data.source);
                    $("#user_input_level").text(data.data.level);
                    $("#user_input_growthValue").text(data.data.growthValue);
                    $("#user_input_registerTime").text(data.data.createTime);
                    $("#user_input_flow").text(data.data.availableDownloadCapacity);
                    $("#user_input_capacity").text(data.data.useCapacity + '/' + data.data.capacity);
                    $("#user_input_shareNumber").text(data.data.freeShareCount);
                    getCurrentUser();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取用户信息失败");
            },
            type: "post"
        });
    }
    $('#userInfoModal').on('show.bs.modal', function () {
        show_userInfo();
    })
    function updateInfo(flag, value) {
        var data;
        if (flag == 1) {
            data = {
                updateFlag: flag,
                nickname: value
            }
        } else if (flag == 2) {
            data = {
                updateFlag: flag,
                password: value
            }
        } else if (flag == 3) {
            data = {
                updateFlag: flag,
                email: value
            }
        } else if (flag == 4) {
            data = {
                updateFlag: flag,
                phone: value
            }
        }
        if (data == null) {
            return;
        }
        $.ajax({
            async: true,
            data: data,
            dataType: "json",
            url: "/user/update",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    if (flag == 1 || flag == 2) {
                        show_userInfo();
                    }
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享移除失败");
            },
            type: "post"
        })
    }

    $("#user_input_nickname").keyup(function () {
        var nickname = $(this).val().replace(/\s+/g, "");
        if (!isEmpty(nickname) && nickname.length >= 3 && nickname.length <= 16 || nickname.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_email").keyup(function () {
        var email = $(this).val().replace(/\s+/g, "");
        if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email) || email.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_phone").keyup(function () {
        var phone = $(this).val().replace(/\s+/g, "");
        if (/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone) || phone.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_email_code").keyup(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (/^\d{6}$/.test(code) || code.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_phone_code").keyup(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (/^\d{6}$/.test(code) || code.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_password").keyup(function () {
        var password = $(this).val().replace(/\s+/g, "");
        if (/^[a-zA-Z]\w{4,15}$/.test(password) || password.length <= 0) {
            $(this).css("border-color", "");
        } else {
            $(this).css("border-color", "red");
        }
    })
    $("#user_input_password_double").keyup(function () {
        var doublePassword = $(this).val().replace(/\s+/g, "");
        var password = $("#user_input_password").val().replace(/\s+/g, "");
        if (doublePassword == password || doublePassword.length <= 0) {
            $("#user_input_password_double").css("border-color", "");
        } else {
            $("#user_input_password_double").css("border-color", "red");
        }
    })

    $("#updateInfo_a_nickname").click(function () {
        var nickname = $("#user_input_nickname").val();
        if (!isEmpty(nickname) && nickname.length >= 3 && nickname.length <= 16) {
            updateInfo(1, nickname);
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("昵称格式有误");
        }
    })
    $("#updateInfo_a_email").click(function () {
        var email = $("#user_input_email").val();
        if (!isEmpty(email) && /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            updateInfo(3, email);
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("邮箱格式有误");
        }
    })
    $("#updateInfo_a_phone").click(function () {
        var phone = $("#user_input_phone").val();
        if (!isEmpty(phone) && /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
            updateInfo(4, phone);
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机格式有误");
        }
    })
    $("#updateInfo_a_password").click(function () {
        var password = $("#user_input_password").val();
        var password_double = $("#user_input_password_double").val();
        if (!isEmpty(password) && !isEmpty(password_double) && password === password_double && /^[a-zA-Z]\w{4,15}$/.test(password)) {
            updateInfo(2, $.md5('zf233' + password));
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("两次密码不一致或密码格式有误，格式:以字母开头的5位至16位字母数字下划线组合");
        }
    })
    $("#rebind_a_phone").click(function () {
        var phone = $("#user_input_phone").val();
        var code = $("#user_input_phone_code").val();
        if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机");
            return;
        }
        if (!/^\d{6}$/.test(code)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            return;
        }
        $.ajax({
            async: true,
            data: {
                phone: phone,
                code: code
            },
            dataType: "json",
            url: "/user/rebind/phone",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("手机绑定失败");
            },
            type: "post"
        })
    })
    $("#rebind_a_email").click(function () {
        var email = $("#user_input_email").val();
        var code = $("#user_input_email_code").val();
        if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("邮箱格式有误");
            return;
        }
        if (!/^\d{6}$/.test(code)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            return;
        }
        $.ajax({
            async: true,
            data: {
                email: email,
                code: code
            },
            dataType: "json",
            url: "/user/rebind/email",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("邮箱绑定失败");
            },
            type: "post"
        })
    })
    $("#applyForLogout").click(function () {
        if (!confirm("确定要申请账号注销吗")) {
            return;
        }
        $.ajax({
            async: true,
            url: "/user/applyForLogout",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    toastr.info("请尽快保存您的文件，已保存请忽略");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取用户信息失败");
            },
            type: "post"
        });
    })
    $("#submit_feedback").click(function () {
        var contact = $("#contact").val().replace(/\s+/g, "");;
        var content = $("#content").val().replace(/\s+/g, "");;
        if (isEmpty(contact) || isEmpty(content)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("请填写联系方式和反馈内容");
            return;
        }
        var flag = confirm("确定要提交反馈吗？");
        if (flag) {
            $.ajax({
                async: true,
                url: "/admin/feedback",
                data: {
                    contact: contact,
                    content: content
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        $("#contact").val("");
                        $("#content").val("");
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function (data) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("反馈发送失败");
                },
                type: "post"
            });
        }
    })
    $("[data-toggle='modal']").click(function () {
        var _target = $(this).attr('data-target')
        if ($(_target).attr("id") === 'user_menu') {
            return;
        }
        t = setTimeout(function () {
            var _modal = $(_target).find(".modal-dialog")
            _modal.animate({ 'margin-top': parseInt(($(window).height() - _modal.height()) / 2) }, 300)
        }, 200)
    })
    $('#file_operation_modal').on('show.bs.modal', function () {
        init_move_form(move_parentId);
    })

    $('#upload_modal').on('show.bs.modal', function () {
        var value = $("#progress").val();
        var max = $("#progress").attr("max");
        var result = value / max;
        if (result > 0.9) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.warning("已用容量超过90%，请及时清理!");
        }
    })

    $(".left_menu").click(function () {
        var id = $(this).attr("id");
        doResize();
        if (id == "recycle") {
            $("#default_menu").hide();
            $("#absolutePath_div").hide();
            $("#share_menu").hide();
            $("#share_table").hide();
            $("#file_table").show();
            $("#recycle_menu").show();
        } else if (id == "share") {
            $("#recycle_menu").hide();
            $("#default_menu").hide();
            $("#absolutePath_div").hide();
            $("#file_table").hide();
            $("#share_table").show();
            $("#share_menu").show();
        } else {
            $("#recycle_menu").hide();
            $("#share_menu").hide();
            $("#share_table").hide();
            $("#file_table").show();
            $("#default_menu").show();
            $("#absolutePath_div").show();
        }
        $(".left_menu").each(function (index, element) {
            var el_id = $(element).attr("id");
            if (id == el_id) {
                $(this).css("background-color", "rgb(245, 245, 245)");
                $('#' + id + ' span').css("font-size", 17);
                $('#' + id + ' span').css("color", "rgb(117, 191, 219)");
            } else {
                $(this).css("background-color", "");
                $('#' + el_id + ' span').css("font-size", 15);
                $('#' + el_id + ' span').css("color", "rgb(126, 135, 141)");
            }
        })
    })
    function doResize() {
        setTimeout(function () {
            //手动触发窗口resize事件
            if (document.createEvent) {
                var event = document.createEvent("HTMLEvents");
                event.initEvent("resize", true, true);
                window.dispatchEvent(event);
            } else if (document.createEventObject) {
                window.fireEvent("onresize");
            }
        }, 100);
    }
    $("#acceptAgree").click(function () {
        if (count > 0) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.info("请在充分阅读并了解该协议内容后同意本协议或注销账户以拒绝接受协议");
            return;
        }
        $.ajax({
            async: true,
            data: {
                agree: 1
            },
            dataType: "json",
            url: "/user/agree",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1500);
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("同意协议失败");
            },
            type: "post"
        })
    })
    $("#noAcceptAgree").click(function () {
        $.ajax({
            async: true,
            data: {
                agree: 0
            },
            dataType: "json",
            url: "/user/agree",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 1500);
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("不同意协议失败");
            },
            type: "post"
        })
    })
})

function initFileInput(ctrlName) {
    var control = $('#' + ctrlName);
    control.fileinput({
        language: 'zh', //设置语言
        uploadUrl: "/file/upload", //上传的地址
        //allowedFileExtensions: ['jpg', 'gif', 'png','exe'],//接收的文件后缀
        //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
        uploadAsync: false, //默认异步上传
        showUpload: true, //是否显示上传按钮
        showRemove: true, //显示移除按钮
        showPreview: true, //是否显示预览
        showCaption: false, //是否显示标题
        browseClass: "btn btn-info", //按钮样式
        //dropZoneEnabled: true,//是否显示拖拽区域
        //minImageWidth: 50, //图片的最小宽度
        //minImageHeight: 50,//图片的最小高度
        //maxImageWidth: 50,//图片的最大宽度
        //maxImageHeight: 50,//图片的最大高度
        previewSettings: {
            image: { width: "88px", height: "88px" },
            html: { width: "88px", height: "88px" },
            text: { width: "88px", height: "88px" },
            video: { width: "88px", height: "88px" },
            audio: { width: "88px", height: "88px" },
            flash: { width: "88px", height: "88px" },
            object: { width: "88px", height: "88px" },
            other: { width: "88px", height: "88px" }
        },
        maxFileSize: 524288, //单位为kb，如果为0表示不限制文件大小
        //minFileCount: 0,
        maxFileCount: 128, //表示允许同时上传的最大文件个数
        enctype: 'multipart/form-data',
        validateInitialCount: true,
        previewFileIcon: "<i class='glyphicon glyphicon-question-sign'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
        layoutTemplates: {
            //actionDelete:'', //去除上传预览的缩略图中的删除图标
            //actionUpload:'',//去除上传预览缩略图中的上传图片；
            //actionZoom:''   //去除上传预览缩略图中的查看详情预览的缩略图标。
        },
    })
}